package com.sky.spring.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Description:
 * 连接工具类
 * @author Sky
 * @Date 2021/7/30 21:51
 */
@Component("connectionUtils")
public class ConnectionUtils {

    @Autowired
    private DataSource dataSource;

    private  ThreadLocal<Connection> threadLocal = new ThreadLocal<>();


    public  Connection getCurrentThreadConn() throws SQLException {
        Connection connection = threadLocal.get();
        if(connection == null){
            connection =  dataSource.getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }
}
